package com.example.activitytest

import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Button
import android.widget.Toast
import kotlinx.android.synthetic.main.first_layout.*

class FirstActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.first_layout)
        button1.setOnClickListener {
            //显示意图
            //val intent =  Intent(this,SecondActivity::class.java)

            //隐式意图
            //val intent = Intent("com.example.activitytest.ACTION_START")

            //请求打开网站
            //val intent = Intent(Intent.ACTION_VIEW)
            //intent.data = Uri.parse("https://www.baidu.com")

            //intent传递参数
            var data = "hello world!"
            var intent = Intent(this,SecondActivity::class.java)
            intent.putExtra("extra_data",data)

            //startActivity(intent)

            //打开Activity并接收返回数据
            startActivityForResult(intent,1)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when(requestCode)
        {
            1 -> if(resultCode== RESULT_OK){
                val returnData = data?.getStringExtra("data_return")
                first_Text_View.text = returnData

            }
        }
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.main,menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when(item.itemId){
            R.id.add_item ->{
                Toast.makeText(this,"添加item",Toast.LENGTH_SHORT).show()
            }
            R.id.remove_item->{
                Toast.makeText(this,"删除item",Toast.LENGTH_SHORT).show()
            }
        }
        return true
    }
}